package com.zx.dsptuneup_cloud.Util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;

public class NumberBaseConversionUtil {
	/**
	 * warnning !!!!数据很小的情况下（max/2）适用，否则不通用
	 *
	 十六进制转换为二进制
	 @param hex 十六进制数
	 @return 二进制数
	 */
	private static String SixToTwo(String hex) {
		//  s注意若是8，则代表负数，1000转为0000
		HashMap<String, String> hexDic = new HashMap<>();
		hexDic.put("0","0000");
		hexDic.put("1","0001");
		hexDic.put("2","0010");
		hexDic.put("3","0011");
		hexDic.put("4","0100");
		hexDic.put("5","0101");
		hexDic.put("6","0110");
		hexDic.put("7","0111");
		hexDic.put("8","1000");
		hexDic.put("9","1001");
		hexDic.put("A","1010");
		hexDic.put("B","1011");
		hexDic.put("C","1100");
		hexDic.put("D","1101");
		hexDic.put("E","1110");
		hexDic.put("F","1111");


		int count =  hex.length();

		String binary = "";
		for (int index = 0; index < hex.length(); index++) {
			String key = hex.substring(index,index+1);
			String value = hexDic.get(key.toUpperCase());
//            if (hex.length() > 1 & index == 0 & key.equals("8")) {

			if (hex.length() > 1 & index == 0 & Integer.parseInt(key, 16)>=8 ) {
				//  符号位1----转为0
				value = changeUnsigned(value);
				value = "-" + value;
			}

			if (value != null) {
				binary = binary + value;
			}
		}
		return binary;
	}
	private static String changeUnsigned(String str){
		String returnStr = "";
		if (str.length()==4){
			for(int i=0;i<4;i++){
				if(i==0){
					returnStr+="0";
				}else {
					returnStr+=str.substring(i,i+1);
				}

			}
		}
		return returnStr;
	}

	/**
	 * @ClassName: valueTenToValueHex(10进制转16进制)
	 * @Description: 不保留前置0，如000000ff==ff
	 * @date 2020年4月15日
	 */
	public static String valueTenToValueHex(String valueTen){
		Integer value= Integer.parseInt(valueTen);
		return  Integer.toHexString(value);
	}
	/**
	 * @ClassName: valueTenToValueHex(10进制转16进制)
	 * @Description: 不保留前置0，如000000ff==ff
	 * @date 2020年4月15日
	 *
	 */
	public static String valueTenToValueHex(Integer valueTen){
		return  Integer.toHexString(valueTen);
	}
	/**
	 * @ClassName: valueTenToValueHex(10进制转16进制)
	 * @Description: 正数正常，负数首位补1
	 * @date 2020年4月15日
	 */
	public static String tenToHexSign(Integer valueTen){
		if (valueTen == null){
			return null;
		}
		if (valueTen >=0){
			return  Integer.toHexString(valueTen);
		}else {
			int temp = valueTen;
			temp =  Math.abs(valueTen);
			temp = temp|0x8000;
			return Integer.toHexString(temp);
		}
	}
	/**
	 * @ClassName: valueTenToValueHex(10进制转16进制)
	 * @Description: 保留前置0，如000000ff
	 * @date 2020年4月15日
	 *
	 */
	public static String valueTenToValueHexStr(String valueTen){
		Integer value= Integer.parseInt(valueTen);
		return  String.format("%08x",value);
	}
	
	/**
	 * @ClassName: valueHexToValueTen(16进制转10进制)
	 * @Description: 参数000000ff和ff都可以
	 * @date 2020年4月15日
	 *
	 */
	public static Integer valueHexToValueTen(String valueHex){
		Integer valueTen2 = Integer.parseInt(valueHex,16);
		return  valueTen2;
	}
	/**
	 * @ClassName: valueHexToValueTen(16进制转10进制)
	 * @Description: 参数000000ff和ff都可以
	 * @date 2020年4月15日
	 *
	 */
	public static Integer valueHexToValueTenSign(String valueHex){
		Integer valueTen2 = Integer.parseInt(valueHex,16);
		String binaryString = Integer.toBinaryString(valueTen2);
		int i = binaryString.length() % 4;
		if (i != 0){
			if (i == 3){
				binaryString = "0"+binaryString;
			}else if (i == 2){
				binaryString = "00"+binaryString;
			}else if (i == 1){
				binaryString = "000"+binaryString;
			}
		}
		char c = binaryString.charAt(0);
		if ('1' == c){
			binaryString = "0"+binaryString.substring(1);
			return - Integer.parseInt(binaryString,2);
		}
		return  Integer.parseInt(binaryString,2);
	}
	/**
	 * @ClassName: valueHexToValueTen(16进制转10进制)
	 * @Description: 参数000000ff和ff都可以
	 * @date 2020年4月15日
	 *
	 */
	public static Long valueHexToValueTenLong(String valueHex){
		Long valueTen2 = Long.parseLong(valueHex,16);
		return  valueTen2;
	}
	/**
	 * @ClassName: valueTwoToValueHex(2进制转16进制)
	 * @Description: 参数000000ff和ff都可以
	 * @date 2020年4月15日
	 *
	 */
	public static String valueTwoToValueHex(String valueTwo){
		Integer valueTen2 = Integer.parseInt(valueTwo,2);
		String strHex = Integer.toHexString(valueTen2);
		return  strHex;
	}
	/**
	 * @ClassName: valueHexToValueTen(16进制转10进制,负数为补码)
	 * @Description: 16进制转10进制,负数为补码 ，参数000000ff和ff都可以
	 * @date 2020年4月15日
	 *
	 */
	public static Integer valueHexToValueTenForComplement(String valueHex){
		Integer value = null;
		char ca = '1';
		String a = "FFFF";
		String substring = a.substring(0, 1);
		int ac = Integer.parseInt(substring, 16);
		if (ac < 8) {
			value = Integer.parseInt(a, 16);
		} else {
			int radix = 2;
			byte[] bytes = ByteToString.toByteArray(a);
			String string = new BigInteger(1, bytes).toString(radix);
			boolean isOk = true;
			if (ca == string.charAt(0)) {
				isOk = false;
			}
			int parseInt = Integer.parseInt(string, 2);
			// string.
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < string.length(); i++) {

				if (ca == string.charAt(i)) {
					sb.append("0");
				} else {
					sb.append("1");
				}
			}
			String afterStr = sb.toString();
			boolean f = false;
			String ss = "";
			for (int i = afterStr.length() - 1; i >= 0; i--) {
				if (!f) {
					if (ca == afterStr.charAt(i)) {
						ss = "0" + ss;
					} else {
						f = true;
						ss = "1" + ss;
					}
				} else {
					ss = afterStr.charAt(i) + ss;
				}
			}
			parseInt = Integer.parseInt(ss, 2);
			if (!isOk) {
				parseInt = ~parseInt + 1;
			}
			value = parseInt;
		}
		return value;
	}


	public static String valueTenDecimalToValueTwo(Double f) {
		StringBuffer  sb = new StringBuffer();
		double dInput = f;
        long longPart = (long) dInput;
        BigDecimal bigDecimal = new BigDecimal(Double.toString(dInput));
        BigDecimal bigDecimalLongPart = new BigDecimal(Double.toString(longPart));
        double dPoint = bigDecimal.subtract(bigDecimalLongPart).doubleValue();
        System.out.println("整数部分为:" + longPart + "\n" + "小数部分为: " + dPoint);
        String p = valueTenToValueHex((int)longPart);
        if (p.length()==1) {
			sb.append("0");
		}
        sb.append(p);
        //处理小数部分
        String v = getValueTwo(dPoint);
        String h = valueTwoToValueHex(v);
        if (h.length()== 1) {
        	sb.append("00000");
		}else if(h.length()== 2){
			sb.append("0000");
		}else if(h.length()== 3){
			sb.append("000");
		}else if(h.length()== 4){
			sb.append("00");
		}else if(h.length()== 5){
			sb.append("0");
		}
        sb.append(h);
        return sb.toString();
	}
	private static String getValueTwo(double d) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 24; i++) {
			d = d*2;
			sb.append((int)d%2);
		}
		return sb.toString();
	}

	public static int SixToDeciTen(String number) {
		//  先转为2进制
		String two = SixToTwo(number);
		return Integer.valueOf(binTodec(two));
	}
	/// 二进制转十进制
	/// - Parameter num: 负数包含-
	private static int binTodec(String num) {
		String temp1 = num;
		boolean flag  = num.contains("-");
		if (flag) {
			temp1 = temp1.replace("-", "");
		}

		int sum = 0;
		char[] c = temp1.toCharArray();

		for(char cc:c){
			String str =  String.valueOf(cc);
			sum = sum * 2 + Integer.valueOf(str).intValue();
		}
		return flag ?  -sum : sum;
	}
}
